var express = require('express');
var router = express.Router();

var mysqlquery = require('../db/mysql')
//用于登录验证
router.get('/', function(req, res, next) {
  res.render('login.html');
});
//检测账号
router.get('/checkuser',  async (req,res)=>{
  let {username} = req.query;
  let sqlstr = `select * from users where username = ${username}`
  let [flag,data] = await mysqlquery(sqlstr);
  if(flag && data.length>=1)
  {
    res.send({status:400,msg:""});
  }
  else
  {
    res.send({status:500,msg:"账号有误"});
  }
})
//检测密码
router.get('/checkpass',async (req,res)=>{
  let {username,password} = req.query;
  let sqlstr = `select * from users where username = ${username}`
  let [flag,data] = await mysqlquery(sqlstr);
  if(flag && data[0].password == password)
  {
    res.send({status:400,msg:""});
  }
  else
  {
    res.send({status:500,msg:"密码有误"});
  }
})
//登录
router.post('/tologin',async (req,res)=>{
   let {tel="",pass=""} = req.body;
   let sqlstr = `select * from users where username = ${tel}`
   let [flag,data] = await mysqlquery(sqlstr);
   if(flag && data[0].password == pass && tel == data[0].username)
   {
     res.send(`<script>alert('登录成功')</script>`);
   }
   else
   {
     res.send(`<script>alert('登录失败');window.location.href='/';</script>`);
   }
})
module.exports = router;
